Objects and classes allow for programmers to break code down even further than functions would allow. They also make it easy to refer to a function or variable that is used often in a program. Objects are all around us, people are objects, cats are objects. These are divided into classes or categories. For example, Ryan is an object from the person class. My dog, bailey, is an object from the dog class.
Each class or category has features and functions that they perform each day. A person will have features such as hair color, height, weight, or Grade Point Average. And functions such as go to school, read, write, or draw. A dog will have similar features and functions but will be set up a little differently than people. You would want to separate dogs from people because it is not very often that you see a person panting or drinking water from a bowl on the ground. Another example class would be a computer class A computer has several features such as processor type, memory size, and screen size.
Now lets look at setting this up in the programming world. In order to get started setting up a class in C# you would click on Project...Add Class...Select the class icon and type person for the name of the class at the bottom of the screen. You should see a new tab at the top. This is now your person class in C#. We can define properties,or features, of a person. I am using age and hair color to start with.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespace WindowsFormsApplication1
{class Person
{publicint age;publicstring hair_color;public Person(){}publicvoid GoToSchool(){//Go straight on Ewing://Turn Right on Fellows://Stop at stop sign://Turn into student lot:}}}
notice there is
class Person
we will concern ourselves with this point down. I declare a few variables to use as our features of a person.
publicint age;publicint hair_color;
Next is something called a constructor. When we create an actual instance of a person which might be named Fred we will call on a function to make a new person. The function called is below.
public Person(){}
This would create an empty person until we specify what color hair and how old the person is later on in the code.
Below is how we would create Fred in the main program.
Person fred =new Person();
This sets Fred up in the program but he still has no age or hair color. To do that the code would look like below.
fred.hair_color = "Brown";
fred.age = 30;
notice we use periods to access the variables that fred has inherited from the person class.
You can also use a function that every person would use such as go to school. Then I could have our person named fred go to school.
fred.GoToSchool();
This would have fred go to school which is really helpful when we start creating a lot of people. We can create a few people called wilma, barney, and betty to go to school without the need to rewrite a lot of code.
Isn't this neat! This makes your code easy to read and very easy to expand upon. Let's say the school moves. All we would need to do is change the directions in the person class and it would not mess with the main code!
Objects
Objects and classes allow for programmers to break code down even further than functions would allow. They also make it easy to refer to a function or variable that is used often in a program. Objects are all around us, people are objects, cats are objects. These are divided into classes or categories. For example, Ryan is an object from the person class. My dog, bailey, is an object from the dog class.
Each class or category has features and functions that they perform each day. A person will have features such as hair color, height, weight, or Grade Point Average. And functions such as go to school, read, write, or draw. A dog will have similar features and functions but will be set up a little differently than people. You would want to separate dogs from people because it is not very often that you see a person panting or drinking water from a bowl on the ground. Another example class would be a computer class A computer has several features such as processor type, memory size, and screen size.
Now lets look at setting this up in the programming world. In order to get started setting up a class in C# you would click on Project...Add Class...Select the class icon and type person for the name of the class at the bottom of the screen. You should see a new tab at the top. This is now your person class in C#. We can define properties,or features, of a person. I am using age and hair color to start with.
notice there is
class Personwe will concern ourselves with this point down. I declare a few variables to use as our features of a person.
Next is something called a constructor. When we create an actual instance of a person which might be named Fred we will call on a function to make a new person. The function called is below.
This would create an empty person until we specify what color hair and how old the person is later on in the code.
Below is how we would create Fred in the main program.
This sets Fred up in the program but he still has no age or hair color. To do that the code would look like below.
notice we use periods to access the variables that fred has inherited from the person class.
You can also use a function that every person would use such as go to school. Then I could have our person named fred go to school.
This would have fred go to school which is really helpful when we start creating a lot of people. We can create a few people called wilma, barney, and betty to go to school without the need to rewrite a lot of code.
Isn't this neat! This makes your code easy to read and very easy to expand upon. Let's say the school moves. All we would need to do is change the directions in the person class and it would not mess with the main code!